<?php
namespace App\Service\Admin\Erqi;

class SucaiService extends BaseService
{
    //素材

    protected $types = [
        1 => '文字', '图片', '视频',
    ];

    /**
     * 列表
     */
    public function getList($type)
    {
        $db = $this->dbConn($this->dbSucai)->where('del', 0);
        if ($type) {
            $db = $db->where('type', $type);
        }
        $total = $db->count();
        $res = $db->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $types = $this->types;
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $tname = array_key_exists($v['type'], $types) ? $types[$v['type']] : '-';
            $imglink = ''; //图片
            $vlink = ''; //视频
            $imgOne = (array) $this->dbConn($this->dbScFile)
                ->where('type', 1)
                ->where('del', 0)
                ->where('sc_id', $v['id'])
                ->orderBy('id','asc')
                ->first();
            if (!empty($imgOne)) {
                $imglink = file_exists($imgOne['link']) ? '/'.$imgOne['link'] : '';
            }
            $vOne = (array) $this->dbConn($this->dbScFile)
                ->where('type', 2)
                ->where('del', 0)
                ->where('sc_id', $v['id'])
                ->orderBy('id','asc')
                ->first();
            if (!empty($vOne)) {
                $vlink = file_exists($vOne['link']) ? '/'.$vOne['link'] : '';
            }
            $arys[] = [
                'id' => $v['id'],
                'tname' => $tname,
                'type' => $v['type'],
                'cnt' => $v['cnt'],
                'imglink' => $imglink,
                'vlink' => $vlink,
                'uptime' => $v['uptime'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    //获取素材
    public function getFiles()
    {
        $arys['imgs'] = [];
        $arys['videos'] = [];
        $res = $this->dbConn($this->dbScFile)
            ->where('del', 0)
            ->where('sc_id', 0)
            ->get();
        if (count($res)) {
            foreach ($res as $v) {
                $v = (array) $v;
                if (file_exists($v['link']) && $v['type'] == 1) {
                    $arys['imgs'][] = '/'.$v['link'];
                }
                if (file_exists($v['link']) && $v['type'] == 2) {
                    $arys['videos'][] = '/'.$v['link'];
                }
            }
        }
        return $arys;
    }

    /**
     * 详情
     */
    public function getOne($id)
    {
        $res = (array) $this->dbConn($this->dbSucai)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        $res['imgs'] = [];
        $res['videos'] = [];
        $scIds = [$id, 0];
        //图片
        $imgOnes = $this->dbConn($this->dbScFile)
            ->where('del', 0)
            ->where('type', 1)
            ->whereIn('sc_id', $scIds)
            ->get();
        if (count($imgOnes)) {
            foreach ($imgOnes as $imgOne) {
                $imgOne = (array) $imgOne;
                if (file_exists($imgOne['link'])) {
                    $res['imgs'][] = '/'.$imgOne['link'];
                }
            }
        }
        //视频
        $videoOnes = $this->dbConn($this->dbScFile)
            ->where('del', 0)
            ->where('type', 2)
            ->whereIn('sc_id', $scIds)
            ->get();
        if (count($videoOnes)) {
            foreach ($videoOnes as $videoOne) {
                $videoOne = (array) $videoOne;
                if (file_exists($videoOne['link'])) {
                    $res['videos'][] = '/'.$videoOne['link'];
                }
            }
        }
        $arys = [
            'id' => $res['id'],
            'type' => $res['type'],
            'cnt' => $res['cnt'],
            'imgs' => $res['imgs'],
            'videos' => $res['videos'],
            'uptime' => $res['uptime'],
        ];
        return $this->toback(0, '', $arys);
    }

    /**
     * 添加
     * @param string $cnt
     */
    public function add($cnt, $type)
    {
        $arys = [
            'cnt' => $cnt,
            'type' => $type,
            'addtime' => toDate(),
            'uptime' => toDate(),
        ];
        $lastid = $this->dbConn($this->dbSucai)->insertGetId($arys);
        if (!$lastid) {
            return $this->toback(-1, '添加失败');
        }
        $this->dbConn($this->dbScFile)->where('sc_id', 0)->update(['sc_id' => $lastid]);
        return $this->toback();
    }

    /**
     * 修改
     * @param int $id
     * @param string $cnt
     */
    public function modify($id, $cnt, $type)
    {
        $res = (array) $this->dbConn($this->dbSucai)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        try {
            $arys = [
                'cnt' => $cnt,
                'type' => $type,
                'uptime' => toDate(),
            ];
            $up = $this->dbConn($this->dbSucai)->where('id', $id)->update($arys);
            if (!$up) {
                return $this->toback(-1, '修改失败');
            }
            $this->dbConn($this->dbScFile)->where('sc_id', 0)->update(['sc_id' => $id]);
            return $this->toback();
        } catch (\Exception $e) {
            echo $e->getMessage();
            return $this->toback(-2, 'error');
        }
    }

    /**
     * 添加文件
     * @param int $scid
     * @param string $img 图片
     * @param int $w 宽度
     * @param int $h 高度
     */
    public function addFile($id, $type, $img)
    {
        //素材库
        $fData = [
            'sc_id' => $id, 'type' => $type, 'link' => $img,
            'addtime' => toDate(),
        ];
        $lastid = $this->dbConn($this->dbScFile)->insertGetId($fData);
        if (!$lastid) {
            return $this->toback(-1, '素材上传失败');
        }
        //图库
        $fData = [
            'link' => $img,
            'addtime' => toDate(),
        ];
        $lastid = $this->dbConn($this->dbImg)->insertGetId($fData);
        if (!$lastid) {
            return $this->toback(-1, '图库失败');
        }
        return $this->toback();
    }

    /**
     * 删除
     * @param int $id
     */
    public function setDel($id)
    {
        $up = $this->dbConn($this->dbSucai)->where('id', $id)
            ->update(['del' => 1, 'deltime' => toDate()]);
        if (!$up) {
            return $this->toback(-1, '删除失败');
        }
        return $this->toback();
    }
}